#include "stdafx.h"
#include "CommandButtonExample.h"

#include "hwinapplication.h"
#include "hwinform.h"
#include "hwinmenu.h"
#include "hwinstdctrls.h"
#include <iostream>

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")

using namespace harlinn::windows;

class CommandButtonForm : public Form
{
    std::shared_ptr<CommandButton> button;
public:
    typedef Form Base;
    CommandButtonForm();
protected:
    virtual void DoOnInitialize();
    virtual void DoOnSize(Message& message);
    virtual void DoOnPaint(Message& message);
};

CommandButtonForm::CommandButtonForm()
    : Base()
{
    SetText(L"CommandButton example");
    SetSize(300,100);
}

void CommandButtonForm::DoOnInitialize()
{
    Base::DoOnInitialize(); 
    auto self = As<CommandButtonForm>();
    button = make_control<CommandButton>(self);
    button->SetText(L"Click me!");
    button->OnClick.connect([&](Control* sender){ SetText(L"Button clicked"); });
}

void CommandButtonForm::DoOnSize(Message& message)
{
    harlinn::windows::Rectangle clientRect = GetClientRect();
    button->MoveWindow(5,5,clientRect.Width()-10,50);
}

void CommandButtonForm::DoOnPaint(Message& message)
{
    Base::DoOnPaint(message);
    auto dc = std::make_shared<PaintDeviceContextHandle>(As<Control>());
    dc->DrawDesktopWallPaper();
}

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR lpCmdLine,int nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);
    try
    {
        auto application = make_component<Application>();
        auto form = make_control<CommandButtonForm>();
        
        auto result = application->Run(form);

        return result;
    }
    catch(std::exception& exc)
    {
        std::cout << exc.what() << std::endl;
    }
    catch(...)
    {
        std::cout << "Unknown exception" << std::endl;    
    }
    return 0;
}